use tracing_subscriber::fmt;
use tracing_subscriber::layer::SubscriberExt;

/// Tracing 日志初始化
///
/// # 日志生成
/// 默认日志将会在当前环境可执行文件执行目录下生产logs文件目录并生成相关的日志文件device_code.log
///
pub(crate) fn tracing_init() -> anyhow::Result<()> {
    // 日志初始化
    let file_appender = tracing_appender::rolling::daily("./logs", "device_code.log");
    // let (non_blocking_writer, _guard) = tracing_appender::non_blocking(file_appender);

    let subscriber = tracing_subscriber::registry()
        .with(fmt::Layer::new().with_writer(std::io::stdout).with_ansi(true))
        .with(fmt::Layer::new().with_writer(file_appender).with_ansi(false))
        .with(tracing_subscriber::filter::LevelFilter::TRACE);

    tracing::subscriber::set_global_default(subscriber).map_err(|err| anyhow::anyhow!(err))
}